﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Moonwish.Core;
using Moonwish.Core.DataAccess;
using Moonwish.Foundation.DataEntity;

namespace Moonwish.Foundation.DataAccess.SqlServer.ValueObjectClassCmd
{
    public class DisableCommand : SqlStringCommand<T_FDT_ValueObjectClass>
    {
        public DisableCommand(DbContext dbContext, List<string> ids, string userId)
            : base(dbContext, ids, userId)
        {
        }

        public override void Execute()
        {
            var status = DataStatus.New.ToString();
            var count = DbTable.Count(obj => obj.DataStatus == status && Ids.Contains(obj.ValueObjectClassId));
            if (count != Ids.Count)
            {
                throw new InvalidOperationException(String.Format("发布集合中包含了无法定位的值对象Id。"));
            }
            DbContext.Database.ExecuteSqlCommand(
                string.Format(
                    "update [T_FDT_ValueObjectClass]  set DataStatus = 'Disabled' where ValueObjectClassId in ('{0}') and DataStatus = '{1}'",
                    string.Join("','", Ids), status));
            DbContext.Database.ExecuteSqlCommand(
                String.Format(
                    "update [T_FDT_ValueObject] set DataStatus = 'Disabled' where ValueObjectClassId in ('{0}') and DataStatus = '{1}'",
                    string.Join("','", Ids), status));
        }
    }
}
